// index.ts
import {saveVxUser,downloadAvatar,uploadAvatar} from '../../api/api'
import { STORAGE_KEY } from '../../api/config'
import { LENGYU_SUCCESS_CODE } from '../../utils/request'
// 获取应用实例
const app = getApp<IAppOption>()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Component({
  data: {
    userInfo: {
      avatarUrl: wx.getStorageSync(STORAGE_KEY.AVATAR)?wx.getStorageSync(STORAGE_KEY.AVATAR):defaultAvatarUrl,
      nickName: wx.getStorageSync(STORAGE_KEY.NICK_NAME)?wx.getStorageSync(STORAGE_KEY.NICK_NAME):"",
    },
    hasUserInfo: false,
    error:"",
  },
  methods: {
    async onChooseAvatar(e: any) {
      console.log(e);
      let result1 = await uploadAvatar(e.detail.avatarUrl,{})
      console.log("文件上传成功",result1)
      this.setData({
        "userInfo.avatarUrl": result1.data,
      })
    },

    onInputChange(e: any) {
      const nickName = e.detail.value
      this.setData({
        "userInfo.nickName": nickName,
      })
    },
    async submit(){
      console.log("确认按钮点击事件")
      if(!this.data.userInfo.nickName || !this.data.userInfo.avatarUrl){
        this.setData({
          error: "请选择头像和昵称"
       })
       return
      }
      let res = await saveVxUser(this.data.userInfo.nickName,this.data.userInfo.avatarUrl);
      if(res.code == LENGYU_SUCCESS_CODE){
        this.nextPage();
        wx.setStorageSync(STORAGE_KEY.AVATAR,this.data.userInfo.avatarUrl);
        wx.setStorageSync(STORAGE_KEY.NICK_NAME,this.data.userInfo.nickName);
      }
      else{

      }
    },
    nextPage(){
      wx.navigateTo({
        url: '../adv/sys-select/index',
      })
    }


  },
})
